<?php

namespace plugin\laoha\app\model;

use plugin\admin\app\model\User;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Comment extends Base
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ha_comments';

    protected $appends = ['html'];

    protected $fillable = ['root','root_id', 'root_type', 'content', 'title', 'user_id', 'parent_id', 'status'];

    protected static function booted()
    {
        static::creating(function ($model) {
            $model->user_id = user_id();
            $maps = plugin_config_merge('morphmaps');
            if(!empty($model->root) && isset($maps[$model->root])){                
                $model->root_type = $maps[$model->root];
            }
            if(is_manage()){  //管理员
                $model->status=0;
            }
        });      
        static::saved(function ($model) {
            $urls = self::get_file_by_content($model->content);
            $urls = array_unique(array_filter($urls));
            foreach($urls as $url){
                $data[] = ['root_id'=>$model->id,'root'=>'comment','root_type'=>'plugin\laoha\app\model\Comment','url'=>$url];
            }
            AttachUrl::where('root_id',$model->id)->where('root','comment')->delete();
            if(!empty($data)){
                AttachUrl::insert($data);
            }
        });             
        static::deleted(function ($model) {
            if ($model->parent_id === 0) {
                $ids = $model->where('parent_id',$model->id)->pluck('id');
                if($ids){
                    $model->where('parent_id', $model->id)->delete();
                    AttachUrl::whereIn('root_id',$ids)->where('root','comment')->delete();
                }
            }
            AttachUrl::where('root_id',$model->id)->where('root','comment')->delete();
        });
    }

	//从内容中提取上传文件
	protected static function get_file_by_content($content=''){
        $pattern = '/!\[.*?\]\((.*?)\)/';
        preg_match_all($pattern, $content, $matches);
        $urls = $matches[1];
        foreach ($urls as $k=>$url) {
            if(stripos($url,'/app/laoha/upload/')!==0){
                unset($urls[$k]);
            }
        }
        return $urls;
	}	



    public function getHtmlAttribute()
    {
        return parsemd($this->content);
    }

    /**
     * 获取父级 模型
     */
    public function root(): MorphTo
    {
        return $this->morphTo();
    }


    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function comments(): HasMany
    {
        return $this->HasMany($this, 'parent_id', 'id')->with('user:id,nickname,avatar')->orderBy('id', 'asc');
    }

    public function reply(): HasMany
    {
        return $this->HasMany($this, 'parent_id', 'id')->where('status',0)->with('user:id,nickname,avatar')->orderBy('id', 'asc');
    }    
 
    public function data_format($data,$type='list'){
        if($type=='list'){
            $data['content'] = parsemd($data['content']);
        }
        return $data;
    }
}
